//
//  NSString+JwUI.m
//  JwPack
//
//  Created by 陈警卫 on 2024-07-16.
//

#import "NSString+JwUI.h"

@implementation NSString (JwUI)

/// 获取字符串尺寸
- (CGSize)jw_sizeForFont:(UIFont *)font expectSize:(CGSize)expectSize lineBreakMode:(NSLineBreakMode)lineBreakMode {
    
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSFontAttributeName] = font;
    
    if (lineBreakMode != NSLineBreakByWordWrapping) {
        NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
        paragraphStyle.lineBreakMode = lineBreakMode;
        attributes[NSParagraphStyleAttributeName] = paragraphStyle;
    }
    
    CGRect rect = [self boundingRectWithSize:expectSize
                                     options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                  attributes:attributes
                                     context:nil];
    return rect.size;
}

/// 获取字符串高度
- (CGFloat)jw_heightForWidth:(CGFloat)width font:(UIFont *)font {
    CGSize expectSize = CGSizeMake(width, MAXFLOAT);
    CGSize size = [self jw_sizeForFont:font expectSize:expectSize lineBreakMode:(NSLineBreakByWordWrapping)];
    return size.height;
}

/// 获取字符串高度
- (CGFloat)jw_heightForWidth:(CGFloat)width fontsize:(CGFloat)fontsize {
    CGFloat height = [self jw_heightForWidth:width font:[UIFont systemFontOfSize:fontsize]];
    return height;
}

/// 获取字符串宽度
- (CGFloat)jw_widthForHeight:(CGFloat)height font:(UIFont *)font {
    CGSize expectSize = CGSizeMake(MAXFLOAT, height);
    CGSize size = [self jw_sizeForFont:font expectSize:expectSize lineBreakMode:(NSLineBreakByWordWrapping)];
    return size.width;
}

/// 获取字符串宽度
- (CGFloat)jw_widthForHeight:(CGFloat)height fontsize:(CGFloat)fontsize {
    CGFloat width = [self jw_widthForHeight:height font:[UIFont systemFontOfSize:fontsize]];
    return width;
}

/// 获取UILabel高度
- (CGFloat)jw_heightForLabelWidth:(CGFloat)width font:(UIFont *)font {
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    lable.text = self; lable.numberOfLines = 0; lable.font = font;
    CGSize size = [lable sizeThatFits:(CGSizeMake(width, MAXFLOAT))];
    return size.height;
}

/// 获取UILabel宽度
- (CGFloat)jw_widthForLabelHeight:(CGFloat)height font:(UIFont *)font {
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, height)];
    lable.text = self; lable.numberOfLines = 0; lable.font = font;
    CGSize size = [lable sizeThatFits:(CGSizeMake(MAXFLOAT, height))];
    return size.width;
}


@end
